home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / string.e < prev    next >
Text File  |  1995-02-11  |  2KB  |  72 lines

  1. OPT MODULE
  2.  
  3. MODULE '*sortobj','*integer'
  4.  
  5. -> string.e: a derived object from 'sortobj' to handle
  6. -> strings.
  7.  
  8. EXPORT OBJECT string OF sortobj
  9.  item
  10.  len:PTR TO integer    -> I made this 'integer' in order to use the cmp in there.
  11. ENDOBJECT
  12.  
  13. -> We need to have a way to initialize 'len'.
  14.  
  15. PROC init() OF string
  16.  DEF tmp:PTR TO integer
  17.  NEW tmp.new()
  18.  self.len := tmp
  19. ENDPROC
  20.  
  21. -> We have to dispose of the string and the 'len' pointer.
  22.  
  23. PROC end() OF string
  24.  DEF tmp:PTR TO integer
  25.  Dispose(self.item) -> get rid of the string.
  26.  tmp:=self.len
  27.  END tmp        -> get rid of its length, too
  28. ENDPROC
  29.  
  30. -> cmp() compares two strings quickly.. but it doesn't handle international characters.
  31. -> Many improvements could be made to this, I'm sure, but it has the virtue of being
  32. -> fairly quick.  Perhaps locale.library support would be nice.
  33.  
  34. EXPORT PROC cmp(item:PTR TO string) OF string
  35.  DEF i,inner,outer
  36.  inner:=self.item
  37.  outer:=item.item
  38.  FOR i := 0 TO IF self.len.lt(item.len) THEN self.len.get()-1 ELSE item.len.get()-1
  39.   IF inner[i] < outer[i] THEN RETURN -1
  40.   IF inner[i] > outer[i] THEN RETURN 1
  41.  ENDFOR
  42.  IF self.len.et(item.len) THEN RETURN 0
  43.  RETURN IF self.len.lt(item.len) THEN -1 ELSE 1
  44. ENDPROC
  45.  
  46. -> set() lets you put a value into the string.
  47.  
  48. EXPORT PROC set(in) OF string
  49.  self.len.set(StrLen(in))
  50.  self.item:=in
  51. ENDPROC
  52.  
  53. -> size() returns the length of the string.
  54.  
  55. EXPORT PROC size() OF string
  56.  RETURN self.len.get()
  57. ENDPROC
  58.  
  59. -> NOTE: here write() and get() are synonyms, since it's already a string.
  60.  
  61. EXPORT PROC write() OF string
  62.  RETURN self.get()
  63. ENDPROC
  64.  
  65. EXPORT PROC get() OF string
  66.  RETURN self.item
  67. ENDPROC
  68.  
  69. -> 'string's unique ID is 1.
  70.  
  71. EXPORT PROC id() OF string IS 1
  72.